package us.codecraft.webmagic.model;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.mortbay.util.MultiMap;
import org.mortbay.util.UrlEncoded;

public class AntStore {

    private static String cate = "http://api.mayiyiyi.com/v1/cate/getcate?";
    private static String catecon = "http://api.mayiyiyi.com/v1/cate/catecon?";
    private static String detail = "http://api.mayiyiyi.com/v1/app/getdetail?";

    private static String vname = "1.2.6";
    private static String channel = "official";
    private static String packagename = "com.ant.store.appstore";
    private static String language = "1";
    private static String model = "HHX";
    private static String no_encrypt = "0";
    private static String brand = "unknown";
    private static String vcode = "13";

    private static String mode = "AES/CBC/PKCS5Padding";
    private static IvParameterSpec ivp = new IvParameterSpec("TVBAO!ds00000000".getBytes());
    private static SecretKeySpec skey = new SecretKeySpec("TVBAO!ds00000000".getBytes(), "AES");

    public static String getCate() throws MalformedURLException {
        return UrlHandle(cate + pj(), true, "UTF-8");
    }

    public static String getCatecon(String cid, String page) throws MalformedURLException {
        return UrlHandle(catecon + pj() + "&page=" + page + "&cid=" + cid, true, "UTF-8");
    }

    public static String getDetail(String id) throws MalformedURLException {
        return UrlHandle(detail + pj() + "&id=" + id, true, "UTF-8");
    }

    private static String pj() {
        return "vname=" + vname + "&channel=" + channel + "&packagename=" + packagename + "&language=" + language
                + "&model=" + model + "&no_encrypt=" + no_encrypt + "&brand=" + brand + "&vcode=" + vcode;
    }

    // 加密
    public static String encrypt(String str) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(mode);
        cipher.init(Cipher.ENCRYPT_MODE, skey, ivp);
        return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes()));
    }

    // 解密
    public static String decrypt(String str) throws NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
        Cipher cipher = Cipher.getInstance(mode);
        cipher.init(Cipher.DECRYPT_MODE, skey, ivp);
        return new String(cipher.doFinal(Base64.getDecoder().decode(str)));
    }

    // url加密
    public static String UrlHandle(String url, boolean isen, String chartSet) throws MalformedURLException {
        URL uri = new URL(url);
        MultiMap multiMap = new MultiMap();
        UrlEncoded.decodeTo(uri.getQuery(), multiMap, chartSet);

        for (Iterator<Entry<String, String>> iterator = multiMap.entrySet().iterator(); iterator.hasNext();) {
            Entry<String, String> ent = (Entry<String, String>) iterator.next();
            try {
                String v = isen ? encrypt(ent.getValue()) : decrypt(ent.getValue());
                // System.out.println(ent.getKey() + "=" + v);
                multiMap.put(ent.getKey(), v);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return url.substring(0, url.indexOf("?") + 1) + UrlEncoded.encode(multiMap, chartSet, true);
    }
}
